package com.gragra.sampling.nonproj.tests;
import static org.junit.Assert.*;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import org.junit.Before;
import org.junit.Test;
import com.gragra.sampling.ThreadedUniformAccess;
import com.gragra.sampling.nonproj.ConvertHeadedFactory;
import com.gragra.sampling.nonproj.DMV;
import com.gragra.sampling.nonproj.generators.CreateDependencyCorpus;
import com.gragra.sampling.nonproj.generators.projective.singleroot.CreateGreedySingleRootProjective;
import com.gragra.sampling.nonproj.structures.nonprojective.NonProjDepdTree;
/**
 * 
 * @author Christoph Teichmann
 * 04.10.2013
 */
public class TestDMV
{
	/**
	 * 
	 */
	private NonProjDepdTree	tree;
	/**
	 * 
	 */
	private ThreadedUniformAccess	tua;
	/**
	 * 
	 */
	private DMV	dmv;
	@Before
	public void setUp() throws Exception
	{
		CreateDependencyCorpus cre = new CreateGreedySingleRootProjective(new ConvertHeadedFactory());
		tree = cre.makeTree("this|0 is|1 a|2 sentence|3 .|4",10,0.1);
		tua = new ThreadedUniformAccess(10);
		IntSet tags = new IntOpenHashSet();
		for(int i=0;i<tree.getLength();++i)
		{tags.add(tree.getTag(i));}
		dmv = new DMV(5, tags, 0.1, 0.5,0.0,0.0);
		tree.initialize(dmv, tua);
	}
	@Test
	public void testRemove()
	{
		double d  = dmv.makeLogProb(tree, 2);
		dmv.add(tree, 2);
		assertTrue(dmv.makeLogProb(tree, 2)>d);
		System.out.println(dmv.getNonLocalLogLike(2));
		dmv.remove(tree, 2);
		assertEquals(dmv.makeLogProb(tree, 2),d,0.0);
		assertEquals(dmv.getLocalLogLike(tree, 2),0.0,0.000001);
		assertEquals(dmv.getNonLocalLogLike(2),0.0,0.000001);
		System.out.println(tree.toString(0));
		tree.resample(dmv, 1.0, 0, tua);
		tree.resample(dmv, 1.0, 0, tua);
		tree.resample(dmv, 1.0, 0, tua);
	}
	@Test
	public void testGetLocalLogLike()
	{assertEquals(dmv.getLocalLogLike(tree, 0),0.0,0.0);}
	@Test
	public void testGetNumberOfEvaluations()
	{
		assertEquals((int) dmv.getNumberOfEvaluations(), 0);
		dmv.makeLogProb(tree, 1);
		dmv.makeLogProb(tree, 3);
		assertEquals((int) dmv.getNumberOfEvaluations(), 10);
	}
}